let _ = require('lodash');

class Viewport {
  constructor(canvas) {
    // let def = {};

    this.Width = canvas.width;
    this.Height = canvas.width;
    // 初始化，宽高=父级，偏移在左上原点
    this.offsetX = 0;
    this.offsetY = 0;

    // 初始化设置背景地图区域size=视窗size，等后续方法传值修改
    // 定义水平/垂直可移动范围，初始化时等于出血线，即不可移动
    // 后续修改时只需要修改[1]的值即可，
    this.Haxis = [this.Width / 2, this.Width / 2];
    this.Vaxis = [this.Height / 2, this.Height / 2];
  }
  setBgMapSize(w, h) {
    if (w < this.Width || h < this.Height) {
      console.log('背景图太小了');
    }
    this.Haxis[1] = w - this.Width;
    this.Vaxis[1] = h - this.Height;
  }

  moveViewport(x, y) {
    let nx = this.offsetX - x;
    let ny = this.offsetY - y;

    this.offsetX = _.clamp(nx, ...this.Haxis);
    this.offsetY = _.clamp(ny, ...this.Vaxis);
  }
  getXY1XY2() {
    let x1 = this.offsetX;
    let y1 = this.offsetY;
    let x2 = this.offsetX + this.Width;
    let y2 = this.offsetY + this.Height;
    return [x1, y1, x2, y2];
  }
  getXYWH() {
    let x = this.offsetX;
    let y = this.offsetY;
    let w = this.Width;
    let h = this.Height;
    return [x, y, w, h];
  }
  getCenter() {
    let x = this.Width / 2 + this.offsetX;
    let y = this.Height / 2 + this.offsetY;
    return { x, y };
  }
}

export { Viewport };
